#include "stdio.h"
#include "math.h"
#include "stdlib.h"
#include "string.h"
long long int String_To_Int(char *string, int Length)
{

    long long int  Temp_Value = 0;
    int i = 0;
    long long int Times_16 = 1;

    //Caculate the length of the string
    printf("The Length is %d\r\n",Length);

    //Convert every character to a value
    for( i = Length - 1 ; i >= 0;i--)
    {
       if((string[i] >= 0x30) && (string[i] <= 0x39))
       {
           Temp_Value += ((string[i] - 0x30)*Times_16);
           Times_16 *= 16;
         } else if((string[i] >= 0x41) &&( string[i] <= 0x46)) { Temp_Value += ((string[i] - (0x41 - 0xa)) * Times_16);
           Times_16 *= 16;
       }
       else if((string[i] >= 0x61) &&( string[i] <= 0x66))
       {
           Temp_Value += ((string[i] - (0x61 - 0xa)) * Times_16);
           Times_16 *= 16;
       }
       else
       {
           printf("Wrong number!\r\n");
           return -1;
       }
       printf(" string[i] is %c and Temp_Value is %llx \r\n",string[i],Temp_Value);
    }
    printf("Temp_Value is %llx\r\n",Temp_Value);

    return Temp_Value;
}
long long int bis(long long int x, long long int m)
{
    //令 a = m - ( 1 << i) 然后将a 的值右移i位的到的值与 m右移i位的值进行比较
    // 如果这两个值相等，说明在减1的时候没有发生借位，则m的bit[i]为1，此时再以同样的方式比较x,如果x的值前后不相等,说明x的bit[i]为0
    // 那么就需要把x的bit[i] 置位，也就是+1

    long long int temp,i,j = 0;
    i = j;


    int a,b = 0;
    a = 0;



    //这里有3种情况
    //x的有效位数比m的有效位数大
    //           等于
    //           小于
    //下面的2个while循环用来计算各自的有效位数
    //for循环中的判断条件要以较小的有效位数为准，同时需要处理的是较大有效位数的数


    temp = 1;
    while( m > temp)
    {
       temp = temp << 1;
       b++;
    }
    temp = 1;
    while( x > temp)
    {
        temp = temp << 1;
        a++;
    }

    if( a >= b)
    {
        printf("b is %x\r\n",b);
        for(; i < b ; i++)
        {
             if( ((m - (1 << i)) >> (i + 1)) == (m >> (i+1)))
            {

                if( ((x - (1 << i)) >> (i + 1)) != (x >> (i+1)))
                {
                     x += (1 << i);
                    printf("x is %llx m is %llx\r\n",x,m);
                 }

             }
        }
        return x;
    }
    else
    {
        printf("a is %x\r\n",a);
        for(; i < a ; i++)
        {
             if( ((x - (1 << i)) >> (i + 1)) == (x >> (i+1)))
            {

                if( ((m - (1 << i)) >> (i + 1)) != (m >> (i+1)))
                {
                     m += (1 << i);
                    printf("x is %llx m is %llx\r\n",x,m);
                 }

             }
        }
        return m;
    }


}
long long int bic(long long int x, long long int m)
{
    x &= ~m;
    return x;
}
long long int bool_or(long long int x, long long int y)
{
    long long int result = bis(x,y);

    return result;

}
long long int bool_xor(long long int x, long long int y)
{

    long long int result = bic(bis(x,y),y);
    return result;
}
int main(int argc, char *argv[])
{
    int *x = 0;
    long long int Temp_Value[2] = {0,};
    long long int value = 0;
    long long int Length = 0;
    int choice = -1;
    printf("argc is %d\r\nargv[2] is %s\r\n",argc,argv[2]);

    if(strcmp(argv[2] ,"or") == 0)
        choice = 1;
    else if(strcmp(argv[2] ,"xor") == 0)
        choice = 2;
    else
        printf("Wrong choice!\r\n");

    if(argc == 4)
    {
        printf(" argv[1] = %s\r\n",argv[1]);
        Length = strlen(argv[1]);
        Temp_Value[0] = String_To_Int(argv[1],Length);

        Length = strlen(argv[3]);
        Temp_Value[1] = String_To_Int(argv[3],Length);

        switch(choice)
        {
            case 1:
                printf("You chose operation \"or\"\r\nThe result is %llx\r\n",bool_or(Temp_Value[0],Temp_Value[1]));
                break;

            case 2:
                printf("You chose operation \"xor\"\r\nThe result is %llx\r\n",bool_xor(Temp_Value[0],Temp_Value[1]));
                break;
        }



    }
    else
    {
        printf("Need more than one argv.\r\n");
        return 0;
    }

        return 0;
}
